package com.company.project.controller;

import com.alibaba.fastjson.JSON;
import com.company.project.bean.RankUser;
import com.company.project.component.excel.export.exception.FileExportException;
import com.company.project.component.excel.fade.ExcelUtil;
import com.company.project.component.excel.importfile.exception.FileImportException;
import com.company.project.core.BaseController;
import com.company.project.core.MsgMap;
import com.company.project.utils.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * IntelliJ IDEA.
 * User: lwb
 * Date: 2017/12/29
 * Time: 13:50
 * function:
 */
@Controller
@RequestMapping
public class IndexController extends BaseController{


    @RequestMapping("/mytest1")
    public void mytest1(HttpServletRequest request , HttpServletResponse response) throws IOException, FileImportException, FileExportException {

        CookieUtil.addCookie(response , "aaa" , "aaa" , 3600);
    }

    @RequestMapping("/mytest2")
    public void mytest2(HttpServletRequest request , HttpServletResponse response) throws IOException, FileImportException, FileExportException {

        //map也可以换成一个实体类
        List<Map> lists = new LinkedList<>();
        for (int i = 0; i < 10; i++)
        {
            Map<String, Object> maps = new HashMap<>();
            maps.put("index", i);
            maps.put("date", DateUtil.yyyyMMddHHmm(new Date()));
            maps.put("greet", "hi" + i);
            maps.put("float", Float.valueOf(i));
            maps.put("bigdecimal", BigDecimal.valueOf(i));
            lists.add(maps);
        }
        ExcelUtil.exportExcelWithList(lists , "exportconfig.xml" , "abc" ,  response);

    }

    @RequestMapping("/testMv")
    public ModelAndView testMv()
    {
        ModelAndView mv = new ModelAndView();
        List<RankUser> list = new ArrayList<>();

        RankUser a = new RankUser();
        a.setScore((float) 2);
        a.setNickName("aaa");
        a.setImageUrl("aa/a.jpg");
        a.setRank(2);

        RankUser r = new RankUser();
        r.setScore((float) 2);
        r.setNickName("aaa");
        r.setImageUrl("aa/a.jpg");
        r.setRank(2);

        RankUser f = new RankUser();
        f.setScore((float) 3);
        f.setNickName("bb");
        f.setImageUrl("abba/ba.jpg");
        f.setRank(2);

        r.getSubUser().add(r);
        r.getSubUser().add(f);

        list.add(a);
        list.add(r);
        mv.addObject("list" , list);
        mv.setViewName("index");
        return  mv;
    }

    @RequestMapping("/")
    public String index()
    {
        return "index";
    }

    @RequestMapping("/login")
    public String login()
    {
        return "login";
    }

    @RequestMapping("/test")
    @ResponseBody
    public MsgMap test(HttpServletRequest request)
    {
        MsgMap msg = this.getMsgMap();
        msg.putUrl("aaa");
        msg.putMsg("lalala");
        System.out.println(PublicUtil.getCurrentMethodName());
        System.out.println(PathUtil.getPorjectPath());
        System.out.println(PathUtil.getClasspath());
        System.out.println(SpringContextHelper.getBean("indexController"));
        return  msg;
    }

}
